package org.zxyj.core.utils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 请求处理工具类
    * @ClassName: RequestUtils
    * @author liuyong
    * @date 2016年8月17日
 */
public class RequestUtils{
	
	public static Map<String, Object> getRequestParams(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Map<String, Object> params = new HashMap<String, Object>();
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			params.put(name, request.getParameter(name));
		}
		return params;
	}
	
	//追加方法 jinll
	public static Map<String, Object> getRequestParamVals(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Map<String, Object> params = new HashMap<String, Object>();
		Enumeration<String> names = request.getParameterNames();
		String [] str = null;
		while(names.hasMoreElements()){
			String name = names.nextElement();
			str = request.getParameterValues(name);
			int length = str.length;
			if(str!=null && length>0){
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < length; i++) {
					sb.append(str[i]);
					if(i<length-1){
						sb.append(",");
					}
				}
				params.put(name, sb.toString());
			}else{
				params.put(name, request.getParameter(name));
			}
			
		}
		return params;
	}
	
	public static Map<String, String> getRequestParamsStr(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Map<String, String> params = new HashMap<String, String>();
		Enumeration<String> names = request.getParameterNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			params.put(name, request.getParameter(name));
		}
		return params;
	}
	
	/**
	 * 获取页面传递的某一个数组值,
	 * @return Class<T>
	 * @throws Exception
	 */
	public static String[] getParaValues(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		String str = request.getParameter(key);
		if(str != null)
			return str.split(",");
		return new String[]{};
	}
	
	/**
	 * 获取页面传递的某一个数组值,
	 * @return Class<T>
	 * @throws Exception
	 */
	public static String getParaValue(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		return request.getParameter(key);
	}
	
	/**
	 * 获取页面传递的某一个数组值,
	 * @return Class<T>
	 * @throws Exception
	 */
	public static String[] getParaValues2(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		String[] str = request.getParameterValues(key);
		return str;
	}
	
	/**
	 * 获取请求中某个attr参数值
	    * @Title: getRequestAttr
	    * @param @param key
	    * @param @return    参数
	    * @return T    返回类型
	    * @throws
	 */
	public static <T> T getRequestAttr(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return (T) request.getAttribute(key);
	}
	/**
	 * 获取请求中传递的某一个数组值,
	 * @return Class<T>
	 * @throws Exception
	 */
	public static String[] getAttrValues(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		String str = (String) request.getAttribute(key);
		if(str != null)
			return str.split(",");
		return new String[]{};
	}
	/**
	 * 获取请求中所有attr参数集合
	    * @Title: getRequestAttrs
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	public static Map<String,Object> getRequestAttrs(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Map<String, Object> params = new HashMap<String, Object>();
		Enumeration<String> names = request.getAttributeNames();
		while(names.hasMoreElements()){
			String name = names.nextElement();
			params.put(name, request.getAttribute(name));
		}
		return params;
	}
	
	/**
	 * 获取请求session中某个attr参数值
	    * @Title: getRequestAttr
	    * @param @param key
	    * @param @return    参数
	    * @return T    返回类型
	    * @throws
	 */
	public static <T> T getSessionAttr(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return (T) request.getSession().getAttribute(key);
	}
	
	/**
	 * 设置请求session中某个attr参数值
	    * @Title: setSessionAttr
	    * @param @param key
	    * @param @param value    参数
	    * @return void    返回类型
	    * @throws
	 */
	public static void setSessionAttr(String key,Object value){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		request.getSession().setAttribute(key, value);
	}
	
	/**
	 * 删除请求session中某个attr参数值
	    * @Title: getRequestAttr
	    * @param @param key
	    * @param @return    参数
	    * @return T    返回类型
	    * @throws
	 */
	public static void removeSessionAttr(String key){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		request.getSession().removeAttribute(key);
	}
	
	/**
	 * 获取非线程安全的session参数集合
	    * @Title: getSessionAttrMap
	    * @param @param key
	    * @param @return    参数
	    * @return ConcurrentHashMap<String,String>    返回类型
	    * @throws
	 */
	public static HashMap<String, Object> getSessionAttrMap(String key){
		Map<String, Object> map = getSessionAttr(key);
		if(map != null){
			return (HashMap<String, Object>) map;
		}
		return new HashMap<String, Object>();
	}
	/**
	 * 获取线程安全的session参数集合
	 * @Title: getCurrSessionAttrMap
	 * @param @param key
	 * @param @return    参数
	 * @return ConcurrentHashMap<String,String>    返回类型
	 * @throws
	 */
	public static ConcurrentHashMap<String, Object> getCurrSessionAttrMap(String key){
		ConcurrentHashMap<String, Object> map = getSessionAttr(key);
		if(map != null){
			return (ConcurrentHashMap<String, Object>) map;
		}
		return new ConcurrentHashMap<String, Object>();
	}
	
}